package org.vaadin.golfscoretracker.ui.controller;

import java.util.List;

import org.vaadin.appfoundation.persistence.facade.FacadeFactory;
import org.vaadin.golfscoretracker.data.Course;
import org.vaadin.golfscoretracker.data.Hole;

import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.data.util.IndexedContainer;

public class CourseController implements DataEditorController<Course> {

    private void setProperties(Item item, Course course) {
        if (course == null) {
            throw new IllegalArgumentException("Course may not be null");
        }

        if (item == null) {
            throw new IllegalArgumentException("Item may not be null");
        }

        item.getItemProperty("name").setValue(course.getName());
        item.getItemProperty("holes").setValue(getHoles(course));
        item.getItemProperty("par").setValue(getPar(course));
    }

    private int getHoles(Course course) {
        if (course == null) {
            return 0;
        }

        if (course.getHoles() == null) {
            return 0;
        }

        return course.getHoles().size();
    }

    private int getPar(Course course) {
        if (course == null) {
            return 0;
        }

        if (course.getHoles() == null) {
            return 0;
        }

        int par = 0;

        for (Hole hole : course.getHoles()) {
            par += hole.getPar();
        }

        return par;
    }

    @Override
    public boolean addItem(Container container, Course pojo) {
        if (pojo == null) {
            return false;
        }

        Item item = container.addItem(pojo);

        if (item == null) {
            return false;
        }

        setProperties(item, pojo);

        return true;
    }

    @Override
    public Course createPojo() {
        return new Course();
    }

    @Override
    public Container getContainer() {
        IndexedContainer container = new IndexedContainer();
        container.addContainerProperty("name", String.class, null);
        container.addContainerProperty("holes", Integer.class, null);
        container.addContainerProperty("par", Integer.class, null);

        List<Course> courses = FacadeFactory.getFacade().list(Course.class);
        if (courses != null) {
            for (Course course : courses) {
                addItem(container, course);
            }
        }

        return container;

    }

    @Override
    public void remove(Course pojo) {
        if (pojo == null) {
            throw new IllegalArgumentException("Course may not be null");
        }

        if (pojo.getId() == null) {
            return;
        }

        FacadeFactory.getFacade().delete(pojo);
    }

    @Override
    public void save(Course pojo) {
        if (pojo == null) {
            throw new IllegalArgumentException("Course may not be null");
        }

        FacadeFactory.getFacade().store(pojo);
    }

    @Override
    public void updateItemProperties(Container container, Course pojo) {
        if (pojo == null) {
            throw new IllegalArgumentException("Course may not be null");
        }

        if (container == null) {
            throw new IllegalArgumentException("Container may not be null");
        }

        Item item = container.getItem(pojo);
        setProperties(item, pojo);
    }

}
